/*
 * Copyright (c) 2012 Supin P Surendran <supinps@gmail.com>.
 *
 * This file is part of InventoryMan.
 *
 * InventoryMan is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * InventoryMan is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with licenseTest.  If not, see <http ://www.gnu.org/licenses/>.
 *
 */
package InventoryMan;

/**
 *
 * @author supinps
 */
public class CustRemove extends javax.swing.JInternalFrame {

    /** Creates new form CustNew
     * @param conn Connection to the InventoryMan Database
     */
    public CustRemove(java.sql.Connection conn) {
        imConnection=conn;
        custNames=new javax.swing.DefaultComboBoxModel(); //It holds data to be displayed on ComboBox
        initComponents();
        initDataSet();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        TxtCode = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        TxtPh = new javax.swing.JTextField();
        TxtMail = new javax.swing.JTextField();
        BnRemove = new javax.swing.JButton();
        BnCancel = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        TxtAddress = new javax.swing.JTextArea();
        CmbName = new javax.swing.JComboBox();

        setTitle("Remove Customer");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/icon_small.png"))); // NOI18N

        jLabel1.setText("Code :");

        TxtCode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TxtCodeActionPerformed(evt);
            }
        });

        jLabel2.setText("Name :");

        jLabel3.setText("Address :");

        jLabel4.setText("Phone :");

        jLabel5.setText("E-Mail :");

        BnRemove.setText("Remove");
        BnRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BnRemoveActionPerformed(evt);
            }
        });

        BnCancel.setText("Cancel");
        BnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BnCancelActionPerformed(evt);
            }
        });

        TxtAddress.setColumns(20);
        TxtAddress.setRows(5);
        jScrollPane1.setViewportView(TxtAddress);

        CmbName.setModel(custNames);
        CmbName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CmbNameActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 608, Short.MAX_VALUE)
                            .addComponent(TxtCode, javax.swing.GroupLayout.DEFAULT_SIZE, 608, Short.MAX_VALUE)
                            .addComponent(TxtPh, javax.swing.GroupLayout.DEFAULT_SIZE, 608, Short.MAX_VALUE)
                            .addComponent(TxtMail, javax.swing.GroupLayout.DEFAULT_SIZE, 608, Short.MAX_VALUE)
                            .addComponent(CmbName, 0, 608, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(BnRemove, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(BnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(TxtCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(CmbName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(TxtPh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(TxtMail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 49, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BnRemove)
                    .addComponent(BnCancel))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BnRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BnRemoveActionPerformed
        try {
           prepStmt=imConnection.prepareStatement("DELETE FROM customer WHERE code=?");
           prepStmt.setInt(1, Integer.parseInt(TxtCode.getText()));
           prepStmt.executeUpdate();
            this.dispose();
        } catch (java.sql.SQLException ex) {
            javax.swing.JOptionPane.showMessageDialog(this, "Communication Error\n"+ex.toString());
        }
    }//GEN-LAST:event_BnRemoveActionPerformed

    private void BnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BnCancelActionPerformed
        this.dispose();
    }//GEN-LAST:event_BnCancelActionPerformed

    private void CmbNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CmbNameActionPerformed
         try {
            //Moving dbCursor to the Chosen Item
            imReply.absolute(CmbName.getSelectedIndex()+1);
            //Data Initialization
            code=Integer.toString(imReply.getInt("code"));
            name=imReply.getString("name");
            address=imReply.getString("address");
            ph=imReply.getString("ph");
            mail=imReply.getString("mail");
            //Initialization Finished
            //Populating Form Fields
            TxtCode.setText(code);
            TxtAddress.setText(address);
            TxtPh.setText(ph);
            TxtMail.setText(mail);
        } catch (java.sql.SQLException ex) {
           javax.swing.JOptionPane.showMessageDialog(this, "Connection Error\n"+ex.toString());
        }
    }//GEN-LAST:event_CmbNameActionPerformed

    private void TxtCodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TxtCodeActionPerformed
        if(TxtCode.isFocusOwner()) {
        code=TxtCode.getText();
        try {
            imReply.beforeFirst();
            while(imReply.next()) {
                if(imReply.getInt("code")==Integer.parseInt(code)) {
                    //Data Initialization
                    name=imReply.getString("name");
                    address=imReply.getString("address");
                    ph=imReply.getString("ph");
                    mail=imReply.getString("mail");
                    //Initialization Finished
                    //Populating Form Fields
                    custNames.setSelectedItem(name);
                    TxtAddress.setText(address);
                    TxtPh.setText(ph);
                    TxtMail.setText(mail);
                }
            }
        } catch (java.sql.SQLException ex) {
            javax.swing.JOptionPane.showMessageDialog(this, "Communication Error!\n"+ex.toString());
        }
        }
    }//GEN-LAST:event_TxtCodeActionPerformed

        @SuppressWarnings("static-access")
    private void initDataSet() {
        try {
            custSuggest.decorate(CmbName);
            stmt = imConnection.createStatement();
            imReply=stmt.executeQuery("SELECT * FROM customer");
            while(imReply.next()) {
                custNames.addElement(imReply.getString("name"));
            }
        } catch (java.sql.SQLException ex) {
            javax.swing.JOptionPane.showMessageDialog(this, "Communication Error !\n"+ex.toString());
        }

    }
    //End of Method definitions

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BnCancel;
    private javax.swing.JButton BnRemove;
    private javax.swing.JComboBox CmbName;
    private javax.swing.JTextArea TxtAddress;
    private javax.swing.JTextField TxtCode;
    private javax.swing.JTextField TxtMail;
    private javax.swing.JTextField TxtPh;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    //Manually Declared Variables
    java.sql.Connection imConnection; //Connection to Database
    java.sql.PreparedStatement prepStmt;
    java.sql.Statement stmt;//Statement to fetch the item's list
    java.sql.ResultSet imReply;//reply containing item lists
    javax.swing.DefaultComboBoxModel custNames;
    org.jdesktop.swingx.autocomplete.AutoCompleteDecorator custSuggest;
    //Declaration of Data Holders
    String code,name,address,ph,mail;
    //End of Declarations

}
